package io.xiaojl.xj2ee.kit;

import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;

import io.xiaojl.xj2ee.common.kit.PageWrapper;
import io.xiaojl.xj2ee.common.xss.SQLFilter;

/**  
 * <p>Title: JpaPageKit</p>  
 *
 * <p>Description: </p>  
 *
 * @author jilong.xiao  
 * @date 2018年9月27日  
 */
public class JpaPageKit {
	
	public static final Pageable getPageable(PageWrapper<?> pw){
		Pageable pageable = PageRequest.of(pw.getCurrPage(), pw.getPageSize());
		Map<String,Object> map = pw.getCondition();
		if(map == null){
			return pageable;
		}
		String sidx = SQLFilter.sqlInject((String)map.get("sidx"));
        String order = SQLFilter.sqlInject((String)map.get("order"));
        if(StringUtils.isNotBlank(sidx) && StringUtils.isNotBlank(order)){
        	Sort sort = new Sort("ASC".equalsIgnoreCase(order)?Direction.ASC:Direction.DESC,sidx);
        	pageable = PageRequest.of(pw.getCurrPage(), pw.getPageSize(), sort);
        }
		return pageable;
	}
}
